package com.example.lianguisystem.service.Impl;

import com.example.lianguisystem.DTO.MaterialRequisitionDTO;
import com.example.lianguisystem.entity.IDraw;
import com.example.lianguisystem.entity.IDrawdetail;
import com.example.lianguisystem.entity.LianGuiLabelRecord;
import com.example.lianguisystem.mapperLianGui.CommonMapper;
import com.example.lianguisystem.mapperLianGui.LabelMapper;
import com.example.lianguisystem.mapperLianGui.MaterialRequisitionMapper;
import com.example.lianguisystem.service.CommonSettingService;
import com.example.lianguisystem.service.MaterialRequisitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class MaterialRequisitionServiceImpl implements MaterialRequisitionService {

    @Autowired
    private CommonSettingService commonSettingService;

    @Autowired
    private MaterialRequisitionMapper materialRequisitionMapper;

    @Autowired
    private CommonMapper commonMapper;
    @Autowired
    private LabelMapper labelMapper;

    @Override
    public IDraw getIDraw(Date date, Map<String, Object> userDataMap, Integer workshop) {
        IDraw iDraw = new IDraw();
        iDraw.setBillkind(1);
        iDraw.setBillstate(0);
        iDraw.setBilldate(date);
        iDraw.setIotypeid(6); // 分切出库类型
        iDraw.setStoreid(1);
        iDraw.setShopid(0);
        iDraw.setOpid(0);
        iDraw.setDepartmentid(null);
        iDraw.setEmpid((Integer) userDataMap.get("empid"));
        iDraw.setRefercount(0);
        iDraw.setUpdatetime(new Date());
        iDraw.setSflag(0);
        iDraw.setWorkshopid(workshop);
        iDraw.setRemark("app领料");
        return iDraw;
    }

    @Override
    public List<IDrawdetail> getIDrawdetail(List<MaterialRequisitionDTO> groupByWorkshop) {
        List<Integer> collect = new ArrayList<>();
        Map<Integer, MaterialRequisitionDTO> billidMap = new HashMap<>();
        for (MaterialRequisitionDTO materialRequisitionDTO : groupByWorkshop) {
            collect.add(materialRequisitionDTO.getBillId());
            billidMap.put(materialRequisitionDTO.getBillId(), materialRequisitionDTO);
        }
        Integer workshop = groupByWorkshop.get(0).getWorkshop();
        List<Map<String, Object>> detailDataList = materialRequisitionMapper.selectDetailData(collect, workshop);
        Set<Object> billidSet = detailDataList.stream().map(map -> map.get("billid")).collect(Collectors.toSet());
        if (billidSet.size() != collect.size()) {
            throw new RuntimeException("记录数量错误，请检查加工单是否审核");
        }
        List<IDrawdetail> resultList = new ArrayList<>();
        for (int i = 0; i < detailDataList.size(); i++) {
            Map<String, Object> detail = detailDataList.get(i);
            Integer mmBillid = (Integer) detail.get("billid");
            String goodsCode = (String) detail.get("GoodsCode");
            MaterialRequisitionDTO materialRequisitionDTO = billidMap.get(mmBillid);
            IDrawdetail iDrawdetail = new IDrawdetail();
            iDrawdetail.setItemno(i + 2);
            Integer goodsid = (Integer) detail.get("goodsid");
            Integer unitid = (Integer) detail.get("unitid");
            iDrawdetail.setGoodsid(goodsid);
            Map<String, Object> priceMap = commonMapper.selectPriceByProd(25, unitid, null, 1, goodsid, 0, 0, 0);
            Double price = ((BigDecimal) priceMap.get("DefaultPrice")).doubleValue();
            iDrawdetail.setAprice(price);
            iDrawdetail.setPrice(price);
            double sum = materialRequisitionDTO.getList().stream().filter(item -> item.getDesignNo().equals(goodsCode)).mapToDouble(LianGuiLabelRecord::getWeight).sum();
            Double amount = sum * price;
            iDrawdetail.setQuantity(sum);
            iDrawdetail.setAmount(amount);
            iDrawdetail.setUnitid(unitid);
            iDrawdetail.setUnitquantity(sum);
            iDrawdetail.setUnitprice(price);
            iDrawdetail.setUnitqty1(sum);
            iDrawdetail.setUnitqty2(0.0);
            iDrawdetail.setUnitqty3(0.0);
            iDrawdetail.setExtqty(0.0);
            iDrawdetail.setReferextqty(0.0);
            iDrawdetail.setRefercount(0);
            iDrawdetail.setReferbilltype(42);
            iDrawdetail.setReferbillid(mmBillid);
            iDrawdetail.setReferitemno((Integer) detail.get("itemno"));
            iDrawdetail.setReferqty(0.0);
            iDrawdetail.setIoqty(0.0);
            iDrawdetail.setReferbillcode((String) detail.get("billcode"));
            iDrawdetail.setSubtag(0);
            resultList.add(iDrawdetail);
        }

        return resultList;
    }

    @Override
    public List<IDrawdetail> getIDrawdetail(String designNO , Double weight) {
        Map<String, Object> prodMap = labelMapper.selectProd(designNO);
        List<IDrawdetail> resultList = new ArrayList<>();
        IDrawdetail iDrawdetail = new IDrawdetail();
        iDrawdetail.setItemno(2);
        Integer goodsid = (Integer) prodMap.get("goodsid");
        Integer unitid = (Integer) prodMap.get("unitid");
        iDrawdetail.setGoodsid(goodsid);
        Map<String, Object> priceMap = commonMapper.selectPriceByProd(25, unitid, null, 1, goodsid, 0, 0, 0);
        Double price = ((BigDecimal) priceMap.get("DefaultPrice")).doubleValue();
        iDrawdetail.setAprice(price);
        iDrawdetail.setPrice(price);
        double sum = weight;
        Double amount = sum * price;
        iDrawdetail.setQuantity(sum);
        iDrawdetail.setAmount(amount);
        iDrawdetail.setUnitid(unitid);
        iDrawdetail.setUnitquantity(sum);
        iDrawdetail.setUnitprice(price);
        iDrawdetail.setUnitqty1(sum);
        iDrawdetail.setUnitqty2(0.0);
        iDrawdetail.setUnitqty3(0.0);
        iDrawdetail.setExtqty(0.0);
        iDrawdetail.setReferextqty(0.0);
        iDrawdetail.setRefercount(0);
        iDrawdetail.setReferbilltype(null);
        iDrawdetail.setReferbillid(null);
        iDrawdetail.setReferitemno(null);
        iDrawdetail.setReferqty(0.0);
        iDrawdetail.setIoqty(0.0);
        iDrawdetail.setReferbillcode(null);
        iDrawdetail.setSubtag(0);
        resultList.add(iDrawdetail);
        return resultList;
    }
}


















